<template>
  <div class="app-container">
    <span style="margin: 10px; width: 70px; text-align: left; display:inline-block;">基本信息</span>
    <el-button type="primary" icon="el-icon-edit" style="margin: 10px;" @click="handleEditBaseInfo()">
      编辑
    </el-button>
    <el-button type="success" icon="el-icon-refresh" style="margin: 10px;" @click="handleRefresh()">
      刷新
    </el-button>
    <el-button type="info" icon="el-icon-circle-close" style="margin: 10px;" @click="handleClose()">
      关闭
    </el-button>
    <el-button type="danger" icon="el-icon-delete" style="margin: 10px;" @click="handleDelete()">
      删除
    </el-button>
    <el-table :data="baseInfoArray" :header-cell-style="headerCellStyle" border stripe element-loading-text="拼命加载中">
      <el-table-column label="字典代码" align="center" min-width="110px">
        <template slot-scope="{row}">
          <span>{{ row.code }}</span>
        </template>
      </el-table-column>
      <el-table-column label="字典名称" align="center" min-width="110px">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="基本信息编辑" :visible.sync="baseInfoEditVisible">
      <el-form ref="baseInfoEditForm" :rules="baseInfoEditRules" :model="baseInfoEdit" label-width="80px" style="width: 600px; margin-left:0px;">
        <el-form-item label="字典代码" prop="code">
          <el-input v-model="baseInfoEdit.code" />
        </el-form-item>
        <el-form-item label="字典名称" prop="name">
          <el-input v-model="baseInfoEdit.name" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="baseInfoEditVisible = false">取消</el-button>
        <el-button type="primary" @click="baseInfoEditConfirm()">确认</el-button>
      </div>
    </el-dialog>

    <span style="margin: 10px; width: 70px; text-align: left; display:inline-block;">字典分类</span>
    <el-button type="primary" icon="el-icon-circle-plus-outline" style="margin: 10px;" @click="handleAddDataDictDetail()">
      添加
    </el-button>
    <el-button type="danger" icon="el-icon-delete" style="margin: 10px;" @click="handleCleanDataDictDetails()">
      清空
    </el-button>
    <el-table :data="dataDictDetails" :header-cell-style="headerCellStyle" border stripe element-loading-text="拼命加载中">
      <el-table-column label="编码" align="center" width="160px">
        <template slot-scope="{row}">
          <span style="float: left">{{ row.code }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" width="160px">
        <template slot-scope="{row}">
          <span style="float: left">{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="说明" align="center" min-width="110px">
        <template slot-scope="{row}">
          <span style="float: left">{{ row.description }}</span>
        </template>
      </el-table-column>
      <el-table-column label="序号" align="center" width="50px">
        <template slot-scope="{row}">
          <span>{{ row.index }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="160px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleEditDataDictDetail(scope)">
            编辑
          </el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" style="margin-left: 10px;" @click="handleDeleteDataDictDetail(scope)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="字典分类添加" :visible.sync="dataDictDetailAddVisible">
      <el-form ref="dataDictDetailAddForm" :rules="dataDictDetailAddRules" :model="dataDictDetailAdd" label-width="50px" style="width: 600px; margin-left:0px;">
        <el-form-item label="编码" prop="code">
          <el-input v-model="dataDictDetailAdd.code" style="width: 200px;" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="dataDictDetailAdd.name" style="width: 200px;" />
        </el-form-item>
        <el-form-item label="说明" prop="description">
          <el-input v-model="dataDictDetailAdd.description" style="width: 200px;" />
        </el-form-item>
        <el-form-item label="序号" prop="index">
          <el-input v-model="dataDictDetailAdd.index" type="number" step="1" :min="0" :max="99" style="width: 100px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dataDictDetailAddVisible = false">取消</el-button>
        <el-button type="primary" @click="dataDictDetailAddConfirm()">确认</el-button>
      </div>
    </el-dialog>
    <el-dialog title="字典分类编辑" :visible.sync="dataDictDetailEditVisible">
      <el-form ref="dataDictDetailEditForm" :rules="dataDictDetailEditRules" :model="dataDictDetailEdit" label-width="50px" style="width: 600px; margin-left:0px;">
        <el-form-item label="编码" prop="code">
          <el-input v-model="dataDictDetailEdit.code" style="width: 200px;" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="dataDictDetailEdit.name" style="width: 200px;" />
        </el-form-item>
        <el-form-item label="说明" prop="description">
          <el-input v-model="dataDictDetailEdit.description" style="width: 200px;" />
        </el-form-item>
        <el-form-item label="序号" prop="index">
          <el-input v-model="dataDictDetailEdit.index" type="number" step="1" :min="0" :max="99" style="width: 100px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dataDictDetailEditVisible = false">取消</el-button>
        <el-button type="primary" @click="dataDictDetailEditComfirm()">确认</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { Message } from 'element-ui'
import { getDataDictById, putDataDictById, deleteDataDictById } from '@/api/system-api'

export default {
  name: 'SystemDataDictionaryDetail',
  filters: {
  },
  data() {
    return {
      dataDict: {
        id: null,
        code: null,
        name: null,
        details: null
      },

      baseInfoArray: null,
      baseInfoEditVisible: false,
      baseInfoEdit: {
        code: null,
        name: null
      },
      baseInfoEditRules: {
        code: [{
          required: true, message: '请输入字典代码', trigger: 'blur'
        }],
        name: [{
          required: true, message: '请输入字典名称', trigger: 'blur'
        }]
      },

      dataDictDetails: null,
      dataDictDetailAddVisible: false,
      dataDictDetailAdd: {
        code: null,
        name: null,
        description: null,
        index: null
      },
      dataDictDetailAddRules: {
        code: [{
          required: true, message: '请输入编码', trigger: 'blur'
        }],
        name: [{
          required: true, message: '请输入名称', trigger: 'blur'
        }],
        index: [{
          required: true, message: '请输入序号', trigger: 'blur'
        }]
      },
      dataDictDetailEditVisible: false,
      dataDictDetailEdit: {
        rowIndex: null,
        code: null,
        name: null,
        description: null,
        index: null
      },
      dataDictDetailEditRules: {
        code: [{
          required: true, message: '请输入编码', trigger: 'blur'
        }],
        name: [{
          required: true, message: '请输入名称', trigger: 'blur'
        }],
        index: [{
          required: true, message: '请输入序号', trigger: 'blur'
        }]
      }

    }
  },
  created() {
    this.loadDetail()
  },
  methods: {
    loadDetail() {
      getDataDictById(this.$route.params.id).then(response => {
        const { data } = response
        this.dataDict = data
        this.baseInfoArray = null
        if (this.dataDict) {
          this.baseInfoArray = [this.dataDict]
        }
        this.dataDictDetails = null
        if (this.dataDict && this.dataDict.details) {
          this.dataDictDetails = this.dataDict.details
        }
      })
    },

    handleEditBaseInfo() {
      Object.assign(this.baseInfoEdit, this.dataDict)
      this.baseInfoEditVisible = true
    },
    baseInfoEditConfirm() {
      this.$refs.baseInfoEditForm.validate((valid) => {
        if (!valid) {
          return
        }
        var data = JSON.parse(JSON.stringify(this.dataDict))
        data.code = this.baseInfoEdit.code
        data.name = this.baseInfoEdit.name
        putDataDictById(this.dataDict.id, data).then(response => {
          this.$notify({
            title: '成功',
            message: '更新成功',
            type: 'success',
            duration: 2000
          })
          this.baseInfoEditVisible = false
          this.loadDetail()
        }).catch(error => {
          Message({
            message: error,
            type: 'error',
            duration: 5 * 1000
          })
        })
      })
    },
    handleRefresh() {
      this.loadDetail()
    },
    handleClose() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    },
    handleDelete() {
      deleteDataDictById(this.dataDict.id).then(response => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        this.handleClose()
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
      })
    },
    headerCellStyle({ row, rowIndex }) {
      return 'background-color: #42b983; color: #FFF;'
    },

    handleAddDataDictDetail() {
      this.dataDictDetailAdd = {
        code: null,
        name: null,
        description: null,
        index: null
      }
      this.dataDictDetailAddVisible = true
    },
    dataDictDetailAddConfirm() {
      this.$refs.dataDictDetailAddForm.validate((valid) => {
        if (!valid) {
          return
        }
        var data = JSON.parse(JSON.stringify(this.dataDict))
        if (data.details) {
          data.details.push(this.dataDictDetailAdd)
        } else {
          data.details = [this.dataDictDetailAdd]
        }
        putDataDictById(this.dataDict.id, data).then(response => {
          this.$notify({
            title: '成功',
            message: '添加成功',
            type: 'success',
            duration: 2000
          })
          this.dataDictDetailAddVisible = false
          this.loadDetail()
        }).catch(error => {
          Message({
            message: error,
            type: 'error',
            duration: 5 * 1000
          })
        })
      })
    },
    handleCleanDataDictDetails() {
      var data = JSON.parse(JSON.stringify(this.dataDict))
      data.details = null
      putDataDictById(this.dataDict.id, data).then(response => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        this.loadDetail()
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
      })
    },
    handleEditDataDictDetail(detailScope) {
      Object.assign(this.dataDictDetailEdit, detailScope.row)
      this.dataDictDetailEdit.rowIndex = detailScope.$index
      this.dataDictDetailEditVisible = true
    },
    dataDictDetailEditComfirm() {
      this.$refs.dataDictDetailEditForm.validate((valid) => {
        if (!valid) {
          return
        }
        var data = JSON.parse(JSON.stringify(this.dataDict))
        var detail = data.details[this.dataDictDetailEdit.rowIndex]
        detail.code = this.dataDictDetailEdit.code
        detail.name = this.dataDictDetailEdit.name
        detail.description = this.dataDictDetailEdit.description
        detail.index = this.dataDictDetailEdit.index
        putDataDictById(this.dataDict.id, data).then(response => {
          this.$notify({
            title: '成功',
            message: '更新成功',
            type: 'success',
            duration: 2000
          })
          this.dataDictDetailEditVisible = false
          this.loadDetail()
        }).catch(error => {
          Message({
            message: error,
            type: 'error',
            duration: 5 * 1000
          })
        })
      })
    },
    handleDeleteDataDictDetail(detailScope) {
      var data = JSON.parse(JSON.stringify(this.dataDict))
      data.details.splice(detailScope.$index, 1)
      putDataDictById(this.dataDict.id, data).then(response => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        this.loadDetail()
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
      })
    }

  }
}

</script>
